<template>
  <div  >

    <el-form :model="form" :rules="rules" ref="form" label-width="100px">

      <el-form-item class="w347" label="旧密码" placeholder="请输入新密码" prop="oldPassword">
        <el-input type="password"  v-model="form.oldPassword"></el-input>
      </el-form-item>

      <el-form-item class="w347" label="新密码" placeholder="请输入新密码" prop="newPassword">
        <el-input type="password"  v-model="form.newPassword"></el-input>
      </el-form-item>

      <el-form-item class="w347" label="确认新密码" placeholder="请输入新密码" prop="confirmNewPassword">
        <el-input type="password"  v-model="form.confirmNewPassword"></el-input>
      </el-form-item>

    </el-form>
    <div align="center" slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button type="primary" @click="update('form')">修 改</el-button>
    </div>

  </div>
</template>

<script>
import { putNewPass } from '@/api/user'
export default {
  data() {
    // const validateOldPass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入旧密码'))
    //   } else if (!(/\d/.test(value))) {
    //     callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
    //   } else if (!(/[a-z]/.test(value))) {
    //     callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
    //   } else {
    //     callback()
    //   }
    // }
    const validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!(/\d/.test(value))) {
        callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
      } else if (!(/[a-z]/.test(value))) {
        callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
      } else if (this.form.oldPassword !== undefined) {
        if (value === this.form.oldPassword) {
          callback(new Error('新密码不能与旧密码相同'))
        } else {
          callback() // 必须确保调用该方法 才会有返回值
        }
      } else {
        // if (this.form.newPassword !== '') {
        //   this.$refs.form.validateField('confirmNewPassword')
        // }
        callback()
      }
    }
    const validateConfirmNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (this.form.newPassword === undefined) {
        this.$refs.form.validateField('newPassword')
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmNewPassword: undefined
      },
      rules: {
        oldPassword: [
          {
            required: true,
            message: '请输入旧密码',
            trigger: 'blur'
          }
        ],
        newPassword: [
          {
            required: true,
            message: '请输入新密码',
            trigger: 'blur'
          },
          {
            min: 8,
            max: 20,
            message: '密码需包含字母以及数字，密码长度在 8 到 20 个字符',
            trigger: 'blur'
          },
          {
            validator: validateNewPass,
            trigger: 'blur'
          }
        ],
        confirmNewPassword: [
          {
            required: true,
            message: '请再次输入新密码',
            trigger: 'blur'
          },
          {
            min: 8,
            max: 20,
            message: '密码需包含字母以及数字，密码长度在 8 到 20 个字符',
            trigger: 'blur'
          },
          {
            validator: validateConfirmNewPass,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        console.log(valid)
        if (valid) {
          putNewPass(this.form).then(response => {
            if (response.message === 'success') {
              this.setFormEmpty()
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'success',
                duration: 2000
              })
              console.log(response.message)
            }
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.setFormEmpty()
    },
    setFormEmpty() {
      this.form = {
        oldPassword: undefined,
        newPassword: undefined,
        confirmNewPassword: undefined
      }
    }
  }
}
</script>


<style lang='scss' scoped>
.w347 {
  width: 100%;
}
</style>
